'use strict';

var mongoose = require('mongoose');
var db = require('../config/db.condig');

module.exports.addArticle = function(params){
  var promise = new mongoose.Promise();

  var article = new db.article(params);

  article.save(function (error, data){
    if(error){
      promise.reject(error);
    }else{
      promise.complete(data);
    }
  });
  return promise;
};


module.exports.getArticle = function(id){
  var promise = new mongoose.Promise();

  db.article.findById(id, function (error, data){
    if(error){
      promise.reject(error);
    }else{
      promise.complete(data);
    }
  });

  return promise;
};


module.exports.deleteArticle = function(id){
  var promise = new mongoose.Promise();

  db.article.findOneAndRemove({_id:id}, function (error, data){
    if(error){
      promise.reject(error);
    }else{
      promise.complete(data);
    }
  });

  return promise;
};


module.exports.updateArticle = function(id, params){
  var promise = new mongoose.Promise();

  db.article.findByIdAndUpdate(id, params, function (error, data){
    if(error){
      promise.reject(error);
    }else{
      promise.complete(data);
    }
  });
  return promise;
};